﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using IRIS.Models;
namespace IRIS.Controllers
{
    public class LinkController : Controller
    {
        //Here we initialize our repository so it will be accsesible to all the functions in this controller 
        DataRepo m_Db = new DataRepo();
        //Here we take an instance of the Link table and a list of the LinkCategory table and return it to the View
        public ActionResult Index()
        {
            LinkViewModel VM = new LinkViewModel{m_linkCategories = m_Db.getLinkCategories(), m_links = m_Db.getLinks()};
            return View(VM);
        }
        //Return all links to the view
        public ActionResult List()
        {
            var model = m_Db.getLinks();
            return View(model);
        }

        //
        // GET: /Link/Create
        //set accesibility
        [Authorize(Roles = "Stjórnendur")]
        public ActionResult Create()
        {
            //Logic from Snorri. 
            //Snorri: This is no logic, THS IS SPARTA!
            
            LinkViewModel linkViewModel = new LinkViewModel { m_linkCategories = m_Db.getLinkCategories(), m_links = m_Db.getLinks(),m_link = m_Db.getLinkById(0),m_linkCategory = m_Db.getLinkCategoryById(0)};
            return View(linkViewModel);
        }

        //
        // POST: /Link/Create
        [Authorize(Roles = "Stjórnendur")]
        [HttpPost]
        public ActionResult Create(LinkViewModel linkDataViews)
        {
            
            //Validate user input - if something is wrong we add a msg to our model - we handle alive in here
            if (string.IsNullOrEmpty(linkDataViews.m_link.URL))
                ModelState.AddModelError("URL", "Þessi reitur verður að vera útfylltur");
            if (string.IsNullOrEmpty(linkDataViews.m_link.Description))
                ModelState.AddModelError("Description", "Þessi reitur verður að vera útfylltur");
            if (string.IsNullOrEmpty(linkDataViews.m_link.Title))
                ModelState.AddModelError("Title", "Þessi reitur verður að vera útfylltur");
            
                 

            linkDataViews.m_link.Alive = true;
            try
            {

                // If everthing is o.k we commit
                if (ModelState.IsValid)
                {
                    m_Db.saveLink(linkDataViews.m_link);
                    return RedirectToAction("Index");
                }
                //return errors if we have any
                
                    return View(linkDataViews);
                
            }
            catch
            {
                //Something is not o.k and we return errors
                return View("NotFound");
            }
        }

        //
        // GET: /Link/Edit/5
        [Authorize(Roles = "Stjórnendur")]
        public ActionResult Edit(int? id)
        {
            if (id.HasValue)
            {
                
                LinkViewModel VM = new LinkViewModel{ m_link = m_Db.getLinkById(id.Value),m_linkCategories = m_Db.getLinkCategories()};
                return View(VM);
            }

            return View("NotFound");
        }

        //
        // POST: /Link/Edit/5
        [Authorize(Roles = "Stjórnendur")]
        [HttpPost]
        public ActionResult Edit(int? id, LinkViewModel link)
        {
            
            


            //Validate that the Link is consistent to our rules 
           if (string.IsNullOrEmpty(link.m_link.URL))
               ModelState.AddModelError("URL", "Þessi reitur verður að vera útfylltur");
           if (string.IsNullOrEmpty(link.m_link.Description))
               ModelState.AddModelError("Description", "Þessi reitur verður að vera útfylltur");
           if (string.IsNullOrEmpty(link.m_link.Title))
               ModelState.AddModelError("Title", "Þessi reitur verður að vera útfylltur");
           
            try
            {

                //Update our Link
                if (ModelState.IsValid)
                { 
                    m_Db.updateLinkById(id.Value,link.m_link);
                    return RedirectToAction("Index");
                }
                return View("NotFound");
            }
            catch
            {
                // Send error msg
                return View();
            }
        }

    }
}